#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Comments

#End Region

Partial Class Usercontrols_Options_Comments
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Protected Property CommentGuid() As Guid
        Get
            Return GetGuidFromVS("CommentID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("CommentID", value)
        End Set
    End Property

    Public Property HideData() As Boolean
        Get
            Return GetBooleanFromVS("HideData")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("HideData", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

        SetPageState()

        If Me.Page.IsPostBack = False Then

            'Set Default Sort Order and Direction
            Me.sortCriteria = "createdate"
            Me.sortDir = "desc"

            LoadPage()

            Me.uxCharCountLabel.Text = "3000"
            Me.uxCommentsTextbox.Attributes.Add("maxCharacters", "3000")
            Me.uxCommentsTextbox.Attributes.Add("onkeyup", "return CharacterCount(this, " & Me.uxCharCountLabel.ClientID & ");")
        End If

        ConfigBaseNavCtrls()
        BindDataItems()

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveComments()
        Dim Comment As Comment = Comment.NewComment

        Comment.itemid = ItemGuid
        Comment.userid = CurrentUserID

        If Not (CommentGuid) = Guid.Empty Then
            Comment.parentid = CommentGuid
        End If

        Comment.subject = Me.uxCommentsSubjectTextBox.Text.Trim
        Comment.comments = Me.uxCommentsTextBox.Text.Trim
        Comment.Save()

        Me.uxCommentsSubjectTextBox.Text = ""
        Me.uxCommentsTextbox.Text = ""
        CommentGuid = Guid.Empty

        BindDataItems()

    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        If Not HideData Then
            If Not (ItemGuid = Guid.Empty) Then
                Dim DS As DataSet
                Dim List As New CommentList

                DS = List.GetCommentParent(ItemGuid)

                Me.RecordCount = DS.Tables(0).Rows.Count

                Me.DataPage.DataSource = DS.Tables(0).DefaultView
                Me.DataPage.AllowPaging = True
                Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
                Me.DataPage.CurrentPageIndex = Me.PageNumber

                CreatePagingLinks()

                With Me.uxCommentsRepeater
                    .DataSource = Me.DataPage
                    .DataBind()
                End With
            End If
        End If

    End Sub

    Private Sub LoadReplies(ByVal Parentid As Guid, ByVal ParentTable As Table, ByVal ParentSubject As String, ByVal Indent As Integer)

        Dim DS As New DataSet
        Dim CommentChild As New CommentList

        DS = CommentChild.GetCommentChild(Parentid)

        If DS.Tables(0).Rows.Count > 0 Then

            Dim Row As DataRow
            Dim T As Table = ParentTable
            Dim R As TableRow = Nothing
            Dim C As TableCell = New TableCell
            Dim TableBody As Table = Nothing
            Dim RowSubject As TableRow = Nothing
            Dim CellSubject As TableCell = Nothing
            Dim RowDetails As TableRow = Nothing
            Dim CellUser As TableCell = Nothing
            Dim CellDetails As TableCell = Nothing

            R = New TableRow

            C.ColumnSpan = 2

            If Indent = 0 Then
                Indent = 10
            Else
                Indent += 10
            End If

            For Each Row In DS.Tables(0).Rows

                TableBody = New Table

                Dim Subject As Label = New Label
                Dim UserLink As New Web.UI.WebControls.HyperLink
                Dim CreateDate As Label = New Label
                Dim Details As Label = New Label
				'Dim Reply As LinkButton = New LinkButton
                Dim ImgAvatar As Image = New Image

				'AddHandler Reply.Command, AddressOf AddComment

                TableBody.Style.Add("margin-left", Indent.ToString & "px")
				'Reply.Text = Resources.Resource.Reply.ToString

                RowSubject = New TableRow
                CellSubject = New TableCell

                RowDetails = New TableRow
                CellUser = New TableCell
                CellDetails = New TableCell

				'Reply.ID = Row("id").ToString

                Subject.Text = String.Format("{0} {1} : {2}", Resources.Resource.InReplyTo.ToString, ParentSubject, Row("subject").ToString.Trim)
                Subject.CssClass = "COMMENTS_SUBJECT"

                UserLink.NavigateUrl = String.Format("{0}.aspx?pid=cp_profileview&userid={1}", AppPage, UserName(Row("userid").ToString().Trim()))
                UserLink.Text = UserName(Row("userid").ToString().Trim())
                CreateDate.Text = FormatCreateDate(Row("createdate").ToString)
                CreateDate.CssClass = "COMMENTS_TINYLIST"

                If EnableSmilies Then
                    LoadSmilies(Details, CurrentUser, SetHTMLReturn(Row("comments").ToString.Trim))
                Else
                    Details.Text = SetHTMLReturn(Row("comments").ToString.Trim)
                End If

                LoadAvatar(ImgAvatar, UserName(Row("userid").ToString().Trim()), False)

                CellSubject.Controls.Add(Subject)
                CellSubject.ColumnSpan = 2
                CellSubject.CssClass = "COMMENTS_TDITEMBODY"
                RowSubject.Cells.Add(CellSubject)

				'Reply.CommandName = "AddComment"
				'Reply.CommandArgument = Row("id").ToString
				'Reply.OnClientClick = "javascript:fGoToAnchor('reply');"
                CellUser.Controls.Add(UserLink)
                CellUser.Controls.Add(LineBreak)
                CellUser.Controls.Add(ImgAvatar)
                CellUser.Controls.Add(LineBreak)
                CellUser.Controls.Add(CreateDate)
                CellUser.Controls.Add(LineBreak)
				'CellUser.Controls.Add(Reply)
                CellUser.CssClass = "COMMENTS_TDITEMTITLE"
                RowDetails.Cells.Add(CellUser)

                CellDetails.Controls.Add(Details)
                CellDetails.CssClass = "COMMENTS_TDITEMBODY"
                RowDetails.Cells.Add(CellDetails)

                TableBody.Rows.Add(RowSubject)
                TableBody.Rows.Add(RowDetails)
                C.Controls.Add(TableBody)

                R.Cells.Add(C)
                T.Rows.Add(R)

				'If CType(Row("childnodecount"), Integer) > 0 Then

				'    LoadReplies(New Guid(Row("id").ToString), ParentTable, Row("subject").ToString, Indent)

				'End If

            Next

            T.CssClass = "COMMENTS_TDITEMBODY"

        End If

    End Sub

    Private Function LineBreak() As Literal
        Dim LB As New Literal

        LB.Text = "<br />"

        Return LB
    End Function

#End Region

#Region " Form Controls Events "

    Protected Sub uxSubmitCommentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitCommentButton.Click

        If Page.IsValid Then
            SaveComments()
        End If

    End Sub

    Protected Sub uxResetCommentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetCommentButton.Click
        Me.uxCommentsSubjectTextBox.Text = ""
        Me.uxCommentsTextbox.Text = ""
        CommentGuid = Guid.Empty
    End Sub

    Protected Sub AddComment(ByVal sender As Object, ByVal e As CommandEventArgs)
        CommentGuid = ConvertToGuid(e.CommandArgument)
        Me.uxCommentsSubjectTextBox.Text = Resources.Resource.RE.ToString
	End Sub

	Protected Sub DeleteComment(ByVal sender As Object, ByVal e As CommandEventArgs)
		CommentGuid = ConvertToGuid(e.CommandArgument)

		Comment.DeleteComment(CommentGuid)

		BindDataItems()

	End Sub


    Protected Sub uxCommentsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxCommentsRepeater.ItemDataBound
        Dim item As RepeaterItem = e.Item

        If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then
            Dim Lbl As Label = CType(e.Item.FindControl("uxCommentsDetails"), Label)
            Dim Img As Image = CType(item.FindControl("uxAvatarImage"), Image)

            LoadAvatar(Img, UserName(e.Item.DataItem("userid").ToString().Trim()), False)

            If EnableSmilies Then
                LoadSmilies(Lbl, CurrentUser, SetHTMLReturn(e.Item.DataItem("comments").ToString))
            Else
                Lbl.Text = SetHTMLReturn(e.Item.DataItem("comments").ToString.Trim)
            End If

            If CType(e.Item.DataItem("childnodecount"), Integer) = 0 Then
                Dim RPH As Panel = CType(e.Item.FindControl("uxRepliesPanelHeader"), Panel)
                Dim RPD As Panel = CType(e.Item.FindControl("uxRepliesPanelDetails"), Panel)

                RPH.Visible = False
                RPH.Enabled = False
                RPD.Visible = False
                RPD.Enabled = False
				'Else
				'Dim T As Table = CType(e.Item.FindControl("uxRepliesTable"), Table)
				'LoadReplies(New Guid(e.Item.DataItem("id").ToString), T, e.Item.DataItem("subject").ToString, 0)
            End If

        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Sub SetPageState()

        Me.uxLoggedInPanel.Visible = IsUserAuthenticated()
        Me.uxPleaseLoginPanel.Visible = Not IsUserAuthenticated()

    End Sub

#End Region

End Class
